package com.usian.admin.controller.v1;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.usian.admin.entity.AdChannel;
import com.usian.admin.entity.AdSensitive;
import com.usian.common.dtos.PageResult;
import com.usian.common.dtos.ResponseResult;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.util.BeanHelper;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.dtos.SensitiveDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.service.IAdSensitiveService;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 敏感词信息表 前端控制器
 * </p>
 *
 * @author usian
 * @since 2022-09-28
 */
@RestController
@RequestMapping("/api/v1/sensitive")
@Api(tags = "敏感词信息表接口")
@CrossOrigin
public class AdSensitiveController{

    @Autowired
    private IAdSensitiveService adSensitiveService;

    /**
     *  分页条件查询敏感词列表
     */
    @PostMapping("list")
    public PageResult<AdSensitive> findByPage(@RequestBody SensitiveDto sensitiveDto){
        return adSensitiveService.findByPage(sensitiveDto);
    }

    /**
     * 新增敏感词
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody SensitiveDto sensitiveDto){
        if(sensitiveDto==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //将dto转为pojo
        AdSensitive adSensitive = BeanHelper.copyProperties(sensitiveDto, AdSensitive.class);
        adSensitive.setCreatedTime(new Date());
        adSensitiveService.save(adSensitive);
        return ResponseResult.ok();
    }

    /**
     * 修改敏感词
     */
    @PutMapping("/update")
    public ResponseResult update(@RequestBody SensitiveDto sensitiveDto){
        if(sensitiveDto==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //将dto转为pojo
        AdSensitive adSensitive = BeanHelper.copyProperties(sensitiveDto, AdSensitive.class);
        adSensitiveService.updateById(adSensitive);
        return ResponseResult.ok();
    }

    /**
     * 删除敏感词
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        if(id==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        adSensitiveService.removeById(id);
        return ResponseResult.ok();
    }
}
